package com.keyancompany.zhongbaopingtai.activity;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.adapter.ImageAdapter;
import com.photoselector.model.PhotoModel;
import com.photoselector.ui.PhotoPreviewActivity;
import com.photoselector.ui.PhotoSelectorActivity;
import com.photoselector.util.CommonUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.BindString;
import butterknife.OnClick;
import butterknife.OnItemClick;

/**
 * 创建圈子话题
 */
public class PublishCircleThemeActivity extends BaseActivity {

    public static final int MAX_LENGTH = 9;
    @BindString(R.string.publish_circle_theme_text)
    String title;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.add_image)
    ImageView addImage;
    @Bind(R.id.image_gridview)
    GridView imageGridview;

    List<PhotoModel> photos = new ArrayList<>();
    private ImageAdapter mAdapter;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_publish_circle_theme;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, title);
    }

    @Override
    protected void setUpViews() {
        mAdapter = new ImageAdapter(mContext, photos);
        imageGridview.setAdapter(mAdapter);

    }

    @OnItemClick(R.id.image_gridview)
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(mContext, PhotoPreviewActivity.class);
        intent.putExtra("photos", (Serializable) photos);
        intent.putExtra("position", position);
//        CommonUtils.launchActivity(mContext, PhotoPreviewActivity.class, );
        startActivity(intent);
    }

    @OnClick(R.id.add_image)
    public void selectPhoto(View view) {
//        PhotoSelectorActivity
//        Intent intent = new Intent(mContext, PhotoSelectorActivity.class);
//        intent.putExtra("maxLength", 9);
        CommonUtils.launchActivityForResult(this, PhotoSelectorActivity.class, Constants.REQUESTCODE, MAX_LENGTH - mAdapter.getCount());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constants.REQUESTCODE && resultCode == RESULT_OK) {
            if (data != null && data.getExtras() != null) {
                photos.addAll((List<PhotoModel>) data.getExtras()
                        .getSerializable("photos"));
                if (photos == null || photos.isEmpty())
                    return;
                mAdapter.notifyDataSetChanged();
//                StringBuffer sb = new StringBuffer();
//                for (PhotoModel photo : photos) {
//                    sb.append(photo.getOriginalPath());
//                }
//                CommonUtils.launchCropActivity(this, Constants.CROPCODE,
//                        sb.toString());

            }
        }
    }
}
